package com.calvin.study.multithread.thread;

import java.util.concurrent.TimeUnit;

import com.calvin.study.utils.ThreadUtil;

public class Thread03 {

	public static void main(String[] args) {
		//模拟两个线程，并行执行，B线程结束，不会影响主线程和A线程。
		new Thread(() -> {
			try {
				for (int i = 0; i < 10; i++) {
					TimeUnit.SECONDS.sleep(1);
					System.out.println("线程：" + Thread.currentThread().getName() + "\t hello,world");
				}
			} catch (Exception e) {
			}

		}, "Thread-A").start();

		new Thread(() -> {
			try {
				for (int i = 0; i < 5; i++) {
					TimeUnit.SECONDS.sleep(1);
					System.out.println("线程：" + Thread.currentThread().getName() + "\t hi,thread");
				}
			} catch (Exception e) {
			}
		}, "Thread-B").start();
	}
}

class Demo01 extends ThreadUtil {

	public static final int MAX_TURN = 5;
	static int threadNo = 1;

	public static void main(String args[]) throws InterruptedException {
		Thread thread = null;
		// 使用Runnable的匿名类创建和启动线程
		for (int i = 0; i < 2; i++) {
			thread = new Thread(new Runnable() { // ①匿名实例
				@Override
				public void run() { // ②异步执行的业务逻辑
					for (int j = 1; j < MAX_TURN; j++) {
						System.out.println(getCurThreadName() + ", 轮次： " + j);
					}
					System.out.println(getCurThreadName() + " 运行结束.");
				}
			}, "RunnableThread" + threadNo++);
			thread.start();
		}
		System.out.println(getCurThreadName() + " 运行结束.");
	}
}
